<template>
  <div class="image-upload-test-container">
    <h2>图片上传测试页面</h2>
    
    <!-- 上传测试 -->
    <el-card class="test-card">
      <h3>出库单缩略图上传测试</h3>
      
      <el-upload
        class="upload-demo"
        action="#"
        :show-file-list="false"
        :before-upload="beforeUpload"
        :http-request="uploadFile"
        accept="image/*"
      >
        <el-button type="primary">选择图片文件</el-button>
        <template #tip>
          <div class="el-upload__tip">
            只能上传jpg/png/gif文件，且不超过2MB
          </div>
        </template>
      </el-upload>
      
      <div v-if="uploadResult" class="upload-result">
        <h4>上传结果:</h4>
        <div class="result-info">
          <p><strong>状态码:</strong> {{ uploadResult.code }}</p>
          <p><strong>消息:</strong> {{ uploadResult.msg }}</p>
          <p><strong>成功:</strong> {{ uploadResult.data?.success ? '是' : '否' }}</p>
          <p><strong>文件路径:</strong> {{ uploadResult.data?.filePath }}</p>
          <p><strong>文件名:</strong> {{ uploadResult.data?.fileName }}</p>
          <p><strong>文件大小:</strong> {{ uploadResult.data?.fileSize }} 字节</p>
          <p><strong>错误信息:</strong> {{ uploadResult.data?.errorMessage || '无' }}</p>
        </div>
        
        <div v-if="uploadResult.data?.filePath" class="image-preview">
          <h4>图片预览:</h4>
          <img :src="getImageUrl(uploadResult.data.filePath)" class="preview-image" alt="上传的图片" />
          <p><strong>完整URL:</strong> {{ getImageUrl(uploadResult.data.filePath) }}</p>
        </div>
      </div>
    </el-card>
    
    <!-- 常见测试图片 -->
    <el-card class="test-card">
      <h3>测试图片</h3>
      
      <div class="test-images">
        <div class="test-image-item">
          <h4>小图片 (1KB以下)</h4>
          <img src="" class="test-image" alt="小图片" />
        </div>
        
        <div class="test-image-item">
          <h4>中等图片 (100KB左右)</h4>
          <img src="" class="test-image" alt="中等图片" />
        </div>
        
        <div class="test-image-item">
          <h4>大图片 (1MB左右)</h4>
          <img src="" class="test-image" alt="大图片" />
        </div>
      </div>
    </el-card>
    
    <!-- 错误测试 -->
    <el-card class="test-card">
      <h3>错误测试</h3>
      
      <div class="error-tests">
        <el-button @click="testLargeFile" type="warning">测试超大文件</el-button>
        <el-button @click="testInvalidFormat" type="danger">测试无效格式</el-button>
        <el-button @click="testEmptyFile" type="info">测试空文件</el-button>
      </div>
      
      <div v-if="errorResult" class="error-result">
        <h4>错误结果:</h4>
        <div class="result-info">
          <p><strong>错误类型:</strong> {{ errorResult.type }}</p>
          <p><strong>错误信息:</strong> {{ errorResult.message }}</p>
          <p><strong>详细信息:</strong> {{ errorResult.details }}</p>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { uploadOutboundThumbnail } from '@/api/outboundOrder'

const uploadResult = ref<any>(null)
const errorResult = ref<any>(null)

// 上传前检查
const beforeUpload = (file: File) => {
  const isImage = file.type.startsWith('image/')
  const isLt2M = file.size / 1024 / 1024 < 2

  if (!isImage) {
    ElMessage.error('只能上传图片文件!')
    return false
  }
  if (!isLt2M) {
    ElMessage.error('图片大小不能超过 2MB!')
    return false
  }
  return true
}

// 文件上传
const uploadFile = async (options: any) => {
  try {
    const file = options.file
    console.log('开始上传文件:', file.name, '大小:', file.size)
    
    const response = await uploadOutboundThumbnail(file)
    
    uploadResult.value = response
    errorResult.value = null
    
    if (response.code === 200 && response.data.success) {
      ElMessage.success('图片上传成功')
    } else {
      ElMessage.error(response.msg || '图片上传失败')
    }
  } catch (error: any) {
    console.error('上传失败:', error)
    errorResult.value = {
      type: '上传错误',
      message: error.message || '未知错误',
      details: error.response?.data || error.toString()
    }
    uploadResult.value = null
    ElMessage.error('图片上传失败')
  }
}

// 获取图片URL
const getImageUrl = (url: string): string => {
  if (!url) return ''
  
  // 如果是完整URL，直接返回
  if (url.startsWith('http://') || url.startsWith('https://')) {
    return url
  }
  
  // 如果是相对路径（以/开头），添加基础URL
  if (url.startsWith('/')) {
    return `${window.location.origin}${url}`
  }
  
  // 如果是后端返回的文件路径（如 uploads/outbound/xxx.png），构建完整URL
  if (url.includes('uploads/') || url.includes('outbound/')) {
    return `${window.location.origin}/uploads/${url.replace(/^uploads\//, '')}`
  }
  
  // 其他情况，尝试作为相对路径处理
  return url
}

// 测试超大文件
const testLargeFile = () => {
  errorResult.value = {
    type: '文件大小超限',
    message: '文件大小超过2MB限制',
    details: '请选择小于2MB的图片文件'
  }
  ElMessage.warning('文件大小超过限制')
}

// 测试无效格式
const testInvalidFormat = () => {
  errorResult.value = {
    type: '文件格式错误',
    message: '不支持的文件格式',
    details: '只支持jpg、png、gif等图片格式'
  }
  ElMessage.warning('不支持的文件格式')
}

// 测试空文件
const testEmptyFile = () => {
  errorResult.value = {
    type: '空文件',
    message: '文件为空',
    details: '请选择有效的图片文件'
  }
  ElMessage.warning('文件为空')
}
</script>

<style scoped>
.image-upload-test-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.test-card {
  margin-bottom: 20px;
}

.test-card h3 {
  margin-bottom: 16px;
  color: #303133;
}

.upload-demo {
  margin-bottom: 16px;
}

.upload-result {
  margin-top: 20px;
  padding: 16px;
  background: #f0f9ff;
  border: 1px solid #b3d8ff;
  border-radius: 6px;
}

.result-info p {
  margin: 8px 0;
  word-break: break-all;
}

.image-preview {
  margin-top: 16px;
  text-align: center;
}

.preview-image {
  max-width: 300px;
  max-height: 200px;
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  object-fit: contain;
}

.test-images {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.test-image-item {
  text-align: center;
}

.test-image-item h4 {
  margin-bottom: 8px;
  color: #606266;
}

.test-image {
  width: 100px;
  height: 100px;
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  object-fit: cover;
}

.error-tests {
  margin-bottom: 16px;
}

.error-tests .el-button {
  margin-right: 10px;
  margin-bottom: 10px;
}

.error-result {
  margin-top: 16px;
  padding: 16px;
  background: #fef0f0;
  border: 1px solid #fbc4c4;
  border-radius: 6px;
  color: #f56c6c;
}
</style>



